package com.bestv.utils.file;

import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 根据文件名格式和最近修改日期过滤
 * User: Henry
 * Date: 14-5-7
 */
public class FileFilterByPattern implements FileFilter {
    Pattern pattern;
    long minModified;

    public FileFilterByPattern(String regex) {
        this.minModified = 0L;
        this.pattern = Pattern.compile(regex);
    }

    public FileFilterByPattern(String regex, Integer second) {
        Date now = new Date();
        this.minModified = now.getTime() - 1000L * second;
        this.pattern = Pattern.compile(regex);
    }

    @Override
    public boolean accept(File file) {
        return !file.isDirectory()
                && pattern.matcher(file.getName()).find()
                && file.lastModified() > minModified;
    }
}
